<?php
class kcorderControl extends skymvc{
	public $typeid;
	public $tbs=array(
			1=>"ymdian",
			2=>"koudai"
		);
	public function __construct(){
		parent::__construct();
		$this->loadConfig("table");
		$this->typeid=1;
	}
	
	public function onDefault(){
		
		$where=" typeid=".$this->typeid." AND shopid=".SHOPID;
		$url=APPYMDIAN."?m=kcorder";
		$start=get('per_page','i');
		$limit=50;
		$status=get('status','i');
		$where.=" AND status=$status";
		$url.="&status=$status";
		$option=array(
			"where"=>$where,
			"order"=>"order_id DESC",
			"start"=>$start,
			"limit"=>$limit
		);
		$rscount=true;
		$data=M("kcorder")->select($option,$rscount);
		$pagelist=$this->pagelist($rscount,$limit,$url); 
		if($data){
			foreach($data as $k=>$v){
				$kcids[]=$v['kcid'];
			}
			$kclist=M("kucun")->getListByIds($kcids);
			 
			foreach($data as $k=>$v){
				$v['kc_title']=$kclist[$v['kcid']]['title'];
				$v['num']=M("kcorder_product")->getOne("select sum(buy_num) as ct from ".table("kcorder_product")." where order_id=".$v['order_id']." ");
				$data[$k]=$v;
			}
		}
		$order_status_list=$this->config_item('order_status_list');
		$order_type_list=$this->config_item('order_type_list');
		$order_ispay=$this->config_item('order_ispay');
		$this->smarty->assign(
			array(
				"data"=>$data,
				"order_status_list"=>$order_status_list,
				"order_type_list"=>$order_type_list,
				"order_ispay"=>$order_ispay,
				"pagelist"=>$pagelist
			)
		);
		$this->smarty->display("kcorder/index.html");
	}
	
	public function onSHow(){
		$order_id=get('order_id','i');
		$data=M("kcorder")->selectRow(array("where"=>"order_id=".$order_id));
		if(empty($data)) $this->goall("参数出错",1);
		 $site=M("sites")->selectRow("siteid=".$data['siteid']);
		$order_status_list=$this->config_item('order_status_list');
		$order_type_list=$this->config_item('order_type_list');
		$order_ispay=$this->config_item('order_ispay');
		//获取商品
		$total_num=0;
		$order_product=M("kcorder_product")->select(array("where"=>"order_id=".$data['order_id']));
		if($order_product){
			foreach($order_product as $k=>$v){
				$ids[]=$v['productid'];
			}
			$products=M("product")->getListByIds($ids);
			
			foreach($order_product as $k=>$v){
				$v['title']=$products[$v['productid']]['title'];
				$v['imgurl']=$products[$v['productid']]['imgurl'];
				$v['p_price']=$products[$v['productid']]['price'];
				$total_num+=$v['buy_num'];
				$order_product[$k]=$v;
			}
			$data['total_num']=$total_num;
		}
		$this->smarty->assign(array(
			"data"=>$data,
			"addr"=>$addr,
			"order_status_list"=>$order_status_list,
			"order_type_list"=>$order_type_list,
			"order_ispay"=>$order_ispay,
			"order_product"=>$order_product,
			"site"=>$site,
			"admin"=>$this->admin
		));
		$this->smarty->display("kcorder/show.html");
	}
	
	public function onpronum(){
			
			$order_id=get_post('order_id','i');
			$productid=get_post('productid','i');
			$order=M("kcorder")->selectRow("order_id=".$order_id);
			if(empty($order) or $order['status']!=0){
				$this->goAll("参数出错",1);
			}
			$row=M("kcorder_product")->selectRow("order_id=".$order_id." AND productid=".$productid."");
			$kucun_product=M("kucun_product")->selectRow("shopid=".$order['kcid']." AND productid=".$productid);
			$num=get('num','i');
			if($order['order_id']!=$row['order_id']){
				$this->goAll("参数出错",1);
			}
			if($row['buy_num']+$num<0){
				$this->goAll("产品若无，请取消订单",1);
			}
			if($num>$kucun_product['total_num']){
				$this->goAll("库存不足了，请取消订单",1);
			}
			M("kcorder")->begin();
			$chmoney=$row['price']*$num;
			M("kcorder")->update(array(
				"money"=>$order['money']+$chmoney,
				"gys_money"=>$order['gys_money']+$kucun_product['jh_price']*$num
			),"order_id=".$order_id);
			/**加库存  加夜猫余额 **/
			$kucun=M("kucun")->selectRow("kcid=".$order['kcid']);
			if($kucun['koudai']){
				//口袋库存 则同步
					M("koudai_product")->total_num_add( 
						array(
							"shopid"=>$kucun['koudai'],
							"productid"=>$productid	,
							"total_num"=>-$num,
							"content"=>"[进货数量处理]"
						)
					 );
					 M("kcorder_product")->update(array(
							"buy_num"=>$row['buy_num']+$num,
						),"id=".$row['id']);
			}else{
				M("kcorder_product")->update(array(
					"buy_num"=>$row['buy_num']+$num,
				),"id=".$row['id']);
				 
				M("kucun_product")->total_num_add(
						array(
							"shopid"=>$order['kcid'],
							"productid"=>$productid	,
							"total_num"=>-$row['total_num'],
							"content"=>"[订单数量改变]",
						)
				);
			}
			//店主余额
			/**
			M($this->tbs[$order['typeid']])->addMoney( 
				array(
					"shopid"=>$order['shopid'],
					"balance"=>-$chmoney,
					"content"=>"您进货数量改变，[money]元"
				)
			);
			**/
			//发送推送信息
			$content=date("H:i:s")."您的进货订单".$order['orderno']."数量改变".$num."";
			M("apppush")->sendShop(array(
						"table"=>"kucun",
						"content"=>$content,
						"shopid"=>$order['kcid'],
					));
			//End 推送信息
			M("kcorder")->commit();
			$this->goAll("成功了",0,array(
				"buy_num"=>$row['buy_num']+$num,
				"money"=>$order['money']+$row['price']*$num,
				"total_num"=> M("kcorder_product")->selectOne(array(
					"where"=>"order_id=".$order_id,
					"fields"=>" sum(buy_num) as b"
				)) 
			));
		}
	
	public function onOrder(){
		M("kcorder")->begin();
		$cart=M("kcorder_cart")->select(array(
			"where"=>"shopid=".SHOPID." AND typeid=".$this->typeid." "
		));
		
		if($cart){
			
			$money=0;
			$shops=array();
			foreach($cart as $k=>$v){
				$productids[]=$v['productid'];
				$kcids[]=$v['kcid'];
			}
			$products=M("product")->getListByIds($productids);
			$kclist=M("kucun")->getListByIds($kcids);
			foreach($cart as $k=>$v){
				$shops[$v['kcid']][]=$v;
				$money +=$products[$v['productid']]['price']*$v['amount'];
				$kucun=$kclist[$v['kcid']];
				//处理库存 减数量 
				if($kucun['koudai']){
					//口袋库存 则同步
					M("koudai_product")->total_num_add(
						array(
							"shopid"=>$kucun['koudai'],
							"productid"=>$v['productid']	,
							"total_num"=>-$v['amount']
						)
					);
				}else{
				//减库存
					M("kucun_product")->total_num_add(
						array(
							"shopid"=>$v['kcid'],
							"productid"=>$v['productid'],
							"total_num"=>-$v['amount'],
							"content"=>"[下单]"
						)
					);
				}
				
			}
			$shop=M("ymdian")->selectRow("id=".SHOPID);
			/*if($money>$shop['balance']){
				$this->goAll("金额不足",2);
			}
			*/
			//金钱处理
			/*不处理夜猫店
			M("ymdian")->addMoney(array(
					"shopid"=>$shop['id'],
					"balance"=>-$money,
					"content"=>"您进货下单了，余额减少[money]元"
				));
			*/
			/*按不同供应商下单*/
			foreach($shops as $kcid=>$scart){
				
				$smoney=0;
				$gys_money=0;
				foreach($scart as $v){
					$kucun_product=M("kucun_product")->selectRow("shopid=".$v['kcid']." AND productid=".$v['productid']);
					$smoney +=$products[$v['productid']]['price']*$v['amount'];
					$gys_money +=$kucun_product['jh_price']*$v['amount'];
					 
				}
				$order=array(
					"shopid"=>SHOPID,
					"siteid"=>SITEID,
					"dateline"=>time(),
					"typeid"=>$this->typeid,
					"kcid"=>$kcid,
					"money"=>$smoney,
					"gys_money"=>$gys_money,
					"nickname"=>$shop['nickname'],
					"telephone"=>$shop['telephone'],
					"address"=>$shop['address'],
					"ispay"=>2,
					"orderno"=>"NO2x".SHOPID.time()
				);
				
				$order_id=M("kcorder")->insert($order);
				foreach($scart as $k=>$v){
					$kucun_product=M("kucun_product")->selectRow("shopid=".$v['kcid']." AND productid=".$v['productid']);
					M("kcorder_product")->insert(array(
						"order_id"=>$order_id,
						"shopid"=>SHOPID,
						"kcid"=>$kcid,
						"siteid"=>SITEID,
						"dateline"=>time(),
						"typeid"=>$this->typeid,
						"productid"=>$v['productid'],
						"object_id"=>$v['object_id'],
						"buy_num"=>$v['amount'],
						"price"=>$products[$v['productid']]['price'],
						"gys_price"=>$kucun_product['jh_price']
					));
					 
				}
				
				//发送推送信息
				$content=date("H:i:s")."您有新的进货订单了";
				M("apppush")->sendShop(array(
						"table"=>"kucun",
						"content"=>$content,
						"shopid"=>$kcid,
					));
				//End 推送信息
				
				//手机短信
				if(SMS_KUCUN_ORDER_NEW){
					$smsdata=array(
						"key"=>"order",
						"para"=>array($orderno."(夜猫店进货 ".date("Y-m-d H:i:s").")","",""),
						"content"=>"【".SMS_QIANMING."】".$content
					);
					$kucun_admins=M("kucun_admin")->select(array("where"=>" shopid=".$kcid));
					if($kucun_admins){
						 
						foreach($kucun_admins as $y){
							if(is_tel($y['telephone'])){
								M("smspush")->insert(array(
									"telephone"=>$y['telephone'],
									"dateline"=>time(),
									"content"=>base64_encode(serialize($smsdata))
								));
							}
						}
					}
				}
				//end手机短信
			}
			/*End 按不同供应商下单*/
			M("kcorder_cart")->delete("shopid=".SHOPID." AND typeid=".$this->typeid." ");
			
			
		}
		M("kcorder")->commit();
		$this->goAll("下单成功",0,0);
	}
	
	public function onReceive(){
		
		$order_id=get('order_id','i');
		$data=M("kcorder")->selectRow(array("where"=>" order_id=".$order_id." AND shopid=".SHOPID." "));
		if(empty($data) or $data['status']<2 or $data['isreceived']==2){
			$this->goall("暂时无法收货",1,0,"/index.php");
		}
		M("kcorder")->begin();
		M("kcorder")->update(array("isreceived"=>2)," order_id=".$order_id." ");
		//加店铺产品
		$order_product=M("kcorder_product")->select(array(
			"where"=>"order_id=".$order_id
		));
		if($order_product){
			foreach($order_product as $v){
				M($this->tbs[$data['typeid']]."_product")->total_num_add(array(
					"shopid"=>$v['shopid'],
					"productid"=>$v['productid'],
					"total_num"=>$v['buy_num'],
					"content"=>'[进货]'
				));											
			}
		}
		M("kcorder")->commit();
		$this->goall("已经确认收货",0,$data);
	}
	
	
	/**
		*取消订单
		*/
			
		public function onCancel(){
			
			$typeid=max(1,get_post('typeid','i'));
			$order_id=get_post('order_id','i');
			$data=M("kcorder")->selectRow(array("where"=>"order_id=".$order_id));
			if(empty($data)) $this->goall("参数出错",1);
			if($data['status']>=3) $this->goall("操作失败",1);
			M("kcorder")->begin();
			M("kcorder")->update(array("status"=>10),"order_id=".$order_id);
			 	 
			//退钱给用户
			$shop=M($this->tbs[$data['typeid']])->selectRow(" id=".$data['shopid']);
			/**无需退钱
			M($this->tbs[$data['typeid']])->addMoney(array(
					"shopid"=>$data['shopid'],
					"balance"=>$data['money'],
					"content"=>"您的下单订单".$data['orderno']."取消成了，余额返还[money]元"
				));
			**/	
			//处理库存 加数量
			$order_product=M("kcorder_product")->select(array("where"=>"order_id=".$data['order_id']));
			$kucun=M("kucun")->selectRow("kcid=".$data['kcid']);
			
			foreach($order_product as $k=>$v){
				if($kucun['koudai']){
					//口袋库存 则同步
					M("koudai_product")->total_num_add(
						array(
							"shopid"=>$kucun['koudai'],
							"productid"=>$v['productid']	,
							"total_num"=>$v['buy_num']
						)
					);
				}else{
				//加库存
					
					M("kucun_product")->total_num_add(
						array(
							"shopid"=>$data['kcid'],
							"productid"=>$v['productid']	,
							"total_num"=>$v['buy_num'],
							"content"=>"[下单取消]"
						)
					);
				}
				
			}
			M("kcorder")->commit();
			$this->goall("取消成功");
		}
	
	
}
?>